//
//  XPMainTabController.m
//  SinaWeibo
//
//  Created by 徐攀 on 16/5/11.
//  Copyright © 2016年 徐攀. All rights reserved.
//

#import "XPMainTabController.h"
#import "XPTabBar.h"
#import "XPHomeController.h"
#import "XPMessageController.h"
#import "XPDiscoverController.h"
#import "XPProfileController.h"

@interface XPMainTabController ()

@end

@implementation XPMainTabController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    XPTabBar *customTabBar = [[XPTabBar alloc] init];
    
    [customTabBar setCallBack:^{
        NSLog(@"Block");
    }];
    // 因为tabBar是只读属性，所以只能通过KVC赋值
    [self setValue:customTabBar forKey:@"tabBar"];
    
    [self addAllMyControllers];
}

#pragma mark - 创建并添加子控制器
- (void)addAllMyControllers {
    [self addChildViewController:[[XPHomeController alloc] init] withTitle:@"首页" withImage:@"tabbar_home"];
    [self addChildViewController:[[XPMessageController alloc] init] withTitle:@"消息" withImage:@"tabbar_message_center"];
    [self addChildViewController:[[XPDiscoverController alloc] init] withTitle:@"发现" withImage:@"tabbar_discover"];
    [self addChildViewController:[[XPProfileController alloc] init] withTitle:@"我的" withImage:@"tabbar_profile"];
}

- (void)addChildViewController:(UIViewController *)childController withTitle:(NSString *)title withImage:(NSString *)imageName {
    childController.title = title;
    [self.tabBar setTintColor:[UIColor grayColor]];
    [self.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor orangeColor]}
                                              forState:UIControlStateSelected];
    [self.tabBarItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12]}
                                   forState:UIControlStateNormal];
    
    childController.tabBarItem.image = [UIImage imageNamed:imageName];
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:[imageName stringByAppendingString:@"_selected"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:childController];
    [self addChildViewController:nav];
}


@end
